package com.bst.bsj.portal.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class UUidUtil {

    private static final String DOTS        = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static final String ENCRYPT_KEY = "sy";

    public static String getUid() {
        String uid = UUID.randomUUID().toString();
        uid = uid.replaceAll("-", "");
        return uid;
    }

    private static String padto10(String s) {
        int i = 0;
        char[] chars = new char[10];
        int len = s.length();
        while (i < len) {
            chars[i] = s.charAt(i);
            i++;
        }
        while (i < 10) {
            chars[i++] = getRandomDigit(1).charAt(0);
        }
        return new String(chars);
    }

  
    
    public static String getRandomDigit(int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String getRandomString(int length) {
        String base = DOTS;
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static Long decodeKey(String code) {
        try {
            DESPlus des = new DESPlus(ENCRYPT_KEY);
            Long pid = Long.parseLong(des.decrypt(code));
            return pid;
        } catch (Exception e) {
        }
        return -1l;
    }

    public static String encodKey(String id) {
        try {
            DESPlus des = new DESPlus(ENCRYPT_KEY);
            return des.encrypt(id);
        } catch (Exception e) {
        }
        return null;
    }

    public static void main(String[] args) {
        // Map map = new HashMap();
        // for (int i = 0; i < 10000; i++) {
        // String key = String.valueOf(getRandomNumber10());
        // if (map.containsKey(key)) {
        // System.out.println("****:" + key);
        // } else {
        // map.put(key, "");
        // // System.out.println(key);
        // }
        // }
        // Random random = new Random();
        System.out.println(encodKey("11123"));
    }

    /**
     * 生成ID会重复，使用的时候请判重
     * 
     * @return
     */
    public static String getRandomNumber10() {
        int[] a = { 1, 4, 8, 11, 15, 18 };
        int[] b = { 2, 5, 9, 12, 13, 16 };
        int[] c = { 3, 6, 7, 10, 14, 17 };
        Random random = new Random();
        int num = nextInt(random.nextInt(100));
        return padto10(a[num / 100 % 6] + "" + b[(num / 10) % 10 % 6] + "" + c[num % 10 % 6]);
    }

    private static int nextInt(int index) {
        if (index < 0) {
            throw new IllegalArgumentException("num must be positive number.");
        }
        double x = Math.abs(Math.cos(Math.sqrt(2) * (index + 1)));
        double y = 1.0 * 137 / 13 / (index + 1);
        double z = 17 * index * index + 29 * index + 67;
        y = y - (int) y;
        while (y * 10 < 1)
            y *= 10;
        while (z > 1)
            z /= 7;
        x = x * z + y;
        String str = String.valueOf(x);
        str = str.substring(2);
        int start = index % 13;
        return Integer.parseInt(str.substring(start, start + 3));
    }

}
